package cn.iocoder.yudao.module.trade.controller.admin.orderpostback;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.module.trade.controller.admin.orderpostback.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.orderpostback.OrderPostBackDO;
import cn.iocoder.yudao.module.trade.service.orderpostback.OrderPostBackService;

@Tag(name = "管理后台 - 订单回传")
@RestController
@RequestMapping("/trade/order-post-back")
@Validated
public class OrderPostBackController {

    @Resource
    private OrderPostBackService orderPostBackService;

    @PostMapping("/create")
    @Operation(summary = "创建订单回传")
    @PreAuthorize("@ss.hasPermission('trade:order-post-back:create')")
    public CommonResult<Long> createOrderPostBack(@Valid @RequestBody OrderPostBackSaveReqVO createReqVO) {
        return success(orderPostBackService.createOrderPostBack(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单回传")
    @PreAuthorize("@ss.hasPermission('trade:order-post-back:update')")
    public CommonResult<Boolean> updateOrderPostBack(@Valid @RequestBody OrderPostBackSaveReqVO updateReqVO) {
        orderPostBackService.updateOrderPostBack(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单回传")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:order-post-back:delete')")
    public CommonResult<Boolean> deleteOrderPostBack(@RequestParam("id") Long id) {
        orderPostBackService.deleteOrderPostBack(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单回传")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:order-post-back:query')")
    public CommonResult<OrderPostBackRespVO> getOrderPostBack(@RequestParam("id") Long id) {
        OrderPostBackDO orderPostBack = orderPostBackService.getOrderPostBack(id);
        return success(BeanUtils.toBean(orderPostBack, OrderPostBackRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单回传分页")
    @PreAuthorize("@ss.hasPermission('trade:order-post-back:query')")
    public CommonResult<PageResult<OrderPostBackRespVO>> getOrderPostBackPage(@Valid OrderPostBackPageReqVO pageReqVO) {
        PageResult<OrderPostBackDO> pageResult = orderPostBackService.getOrderPostBackPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderPostBackRespVO.class));
    }

    @GetMapping("/pageByDrainage")
    @Operation(summary = "订单回传统计")
    public CommonResult<PageResult<DrainagePostBackRespVO>> pageByDrainage(@Valid DrainagePostBackPageReqVO pageReqVO) {
        PageResult<DrainagePostBackRespVO> pageResult = orderPostBackService.listByDrainageId(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单回传 Excel")
    @PreAuthorize("@ss.hasPermission('trade:order-post-back:export')")
    public void exportOrderPostBackExcel(@Valid OrderPostBackPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderPostBackDO> list = orderPostBackService.getOrderPostBackPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单回传.xls", "数据", OrderPostBackRespVO.class,
                        BeanUtils.toBean(list, OrderPostBackRespVO.class));
    }

}